var util = require('../../../utils/util.js');
var ajax = require('../../../utils/ajax.js');
var common = require('../../../utils/common.js');
var PageBase = require('../../../common/PageBase.js');
var app = getApp();
var PAGE_SIZE = 10; //活动分页每页数据条数

var page;

Page(common.extendPage(PageBase, {
  data: {

    doommData: [{
      time: "10",
      user: "翔之队",
      imgUrl: "http://wx3.sinaimg.cn/thumb150/005uWJqNly1fne7rhvevnj30qo0qogqa.jpg",
      goods: "购买了千百度新款女鞋"
    },
    {
      imgUrl: "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1520076263362&di=507e726908da364acd32a5ebc01fbe4a&imgtype=0&src=http%3A%2F%2Fwx1.sinaimg.cn%2Fthumb150%2F006ZYKV9gy1fmcn3slkmnj30m80m8djw.jpg",
      time: "12",
      user: "焕然一新",
      goods: "发起2018春季新款衬衫的拼团"
    },
    {
      imgUrl: "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1520088350081&di=fb224168215b3f267c01c089b9c59828&imgtype=0&src=http%3A%2F%2Fwx1.sinaimg.cn%2Fthumb150%2F005SU4tSgy1foje0sncjfj30u00u0dkr.jpg",
      time: "8",
      user: "清风徐来",
      goods: "发起Rols大毛领羽绒服拼团"
    }],

    //是否有红包
    isShowRed: false,
    isShowFid: false,
    dataLoaded: false,
    page: 2,
    pageLoading: false,
    pageEnd: false,
    //banner
    banner: [],
    notice: [],
    ad: {},
    //拼团商品
    ptgoods: [],
    msgoods: [],
    msgoods2: [],
    //热销商品
    pbgoods: [],
    redpackets: {},
    catList:[]
  },

//播放弹幕
  _playdanmu: function(){
    var data = [{
      time: "10",
      user: "翔之队",
      imgUrl: "http://wx3.sinaimg.cn/thumb150/005uWJqNly1fne7rhvevnj30qo0qogqa.jpg",
      goods: "购买了千百度新款女鞋"
    },
    {
      imgUrl: "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1520076263362&di=507e726908da364acd32a5ebc01fbe4a&imgtype=0&src=http%3A%2F%2Fwx1.sinaimg.cn%2Fthumb150%2F006ZYKV9gy1fmcn3slkmnj30m80m8djw.jpg",
      time: "12",
      user: "焕然一新",
      goods: "发起2018春季新款衬衫的拼团"
    },
    {
      imgUrl: "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1520088350081&di=fb224168215b3f267c01c089b9c59828&imgtype=0&src=http%3A%2F%2Fwx1.sinaimg.cn%2Fthumb150%2F005SU4tSgy1foje0sncjfj30u00u0dkr.jpg",
      time: "8",
      user: "清风徐来",
      goods: "发起Rols大毛领羽绒服拼团"
    }];
    setInterval(function(){
        this.setData({
          doommData: []
        });
        this.setData({
          doommData: data
        })
    }.bind(this),10000);
  },

  onShareAppMessage: function (res) {
    return {
      title: "浙江的味道，不一样的味道。",
      path: '/pages/store/index/index',
      success: function (res) {
        // 转发成功
      },
      fail: function (res) {
        // 转发失败
      }
    }
  },

  onLoad(options) {
      common.initApp({
        loginSuc: () => {
          this._fetch();
          //this._playdanmu();
        }
      });
  },

  onPullDownRefresh() {
    wx.showNavigationBarLoading() //在标题栏中显示加载
    this._fetch(() => {
      wx.hideNavigationBarLoading() //完成停止加载
      wx.stopPullDownRefresh();
    });
  },



  onReachBottom() {
  },

  _showRed() {
    ajax.G({
      url: 'm=plugins&c=redpackets',
      data: {},
      success: (res) => {
        var redpackets = {
          packetsname: res.data.packetsname,
          money: res.data.money
        };
        this.setData({
          redpackets
        });
        if (Object.keys(res.data).length != 0){
          var time = setTimeout(function () {
            this.setData({
              isShowRed: true
            })
          }.bind(this), 1000);
        };
        this._showRed2();
      },
      fail: () => {
        
      }
    });
    
  },

  _close_red(){
    this.setData({
      isShowRed: false
    })
  },

  _showRed2() {
    ajax.G({
      url: 'm=plugins&c=fissionpacket&a=getfission',
      data: {},
      success: (res) => {
        this.setData({
          fid: res.data.id
        });
        if (Object.keys(res.data).length != 0) {
          var time = setTimeout(function () {
            this.setData({
              isShowFid: true
            })
          }.bind(this), 1000);
        }
      },
      fail: () => {

      }
    });

  },

  _close_red() {
    this.setData({
      isShowRed: false
    })
  },

  _close_red2() {
    this.setData({
      isShowFid: false
    })
  },

  /**
   * 拉取页面数据
   * @return {[type]} [description]
   */
  _fetch(cb) {
    ajax.G({
      url: 'm=shop&c=index&a=indexsetting&id=0',
      data: {},
      success: (res) => {
        var {
          banner,
          lowprice,
          salead,
          secondsale,
          todaysale,
          floor,
          bannerids,
          activity,
          saleadids,
          lowpriceids,
          secondsaleids,
          todaysaleids,
          navids
        } = res.data;
        //var pageEnd = actList.length < PAGE_SIZE;

        // bannerList = bannerList.map((item) => {
        //   return {
        //     imgUrl: item.image_url
        //   }
        // });
        
        var msgoods2 = [],msgoods3 = [];
        secondsaleids.map((item, index) => {
          if (index > 1){
            msgoods2.push(item)
          }
        });
        secondsaleids.map((item, index) => {
          if (index <= 1) {
            msgoods3.push(item)
          }
        });
        this.setData({
          banner,
          lowprice,
          salead,
          secondsale,
          todaysale,

          dataLoaded: true,
          bannerids: bannerids,
          notice: activity,
          ad: saleadids[0],
          ptgoods: lowpriceids,
          msgoods3,
          msgoods2,
          bpgoods: todaysaleids,
          nav: navids,
          catList:floor
        });
        this._showRed();//进店有礼

        cb && cb();
      },
      fail: () => {
        cb && cb();
      }
    });
  },

  onHide(){
    this.setData({
      isShowRed: false
    })
  },
  _toFloor:function(evt){
    var url = evt.currentTarget.dataset.url;
    var current = evt.currentTarget.dataset.index;
    console.log(current);
    app.setFloorId(current);
    wx.switchTab({
      url
    })
  }

}));